<template>
<div class="remarkBigBox">
  <div class="condition">
    <label>
      评价
      <select @change="getRemark" v-model="searchData.star" class="form-select" aria-label="Default select example">
        <option value="0" selected>全部</option>
        <option value="1">一星</option>
        <option value="2">二星</option>
        <option value="3">三星</option>
        <option value="4">四星</option>
        <option value="5">五星</option>
      </select>
    </label>
    <label>
      搜索
      <input @keyup.enter="getRemark" v-model="searchData.content" type="text" class="form-control" aria-describedby="emailHelp">
    </label>
  </div>
  <div v-if="commentsListData.length" class="remarkItemBox">
    <ul>
      <li v-for="(item,index) in commentsListData" :key="item">
        <div class="remark">
          <div class="remarkHeader">
            <div class="remarkAvatar">
              <img :src="item.user.avatar" alt="">
              <div class="remarkName">
                <div>{{item.user.nickname}}</div>
                <div>{{formatDate(item.comments.time)}}</div>
              </div>
            </div>
            <div style="display: flex">
              <div class="remarkStarsText">星级</div>
              <ul class="remarkStars">
                <li v-for="(star,starIndex) in 5" :key="starIndex">
                  <i v-if="starIndex<item.comments.star" class="bi bi-star-fill"></i>
                  <i v-else class="bi bi-star"></i>
                </li>
              </ul>
            </div>
          </div>
          <div class="remarkBody">
            <div class="remarkSku">
              <span>商品：&nbsp;</span><span>{{orderInfo(item.orders.itemsLists)}}</span>
            </div>
            <p class="remarkText">
              {{item.comments.content}}
            </p>
            <div class="remarkPic">
              <img v-for="img in item.comments.imagesList" :src="img" alt="" :key="img">

            </div>
            <div class="storeResponse" v-if="item.comments.storeResponse">
              <p class="responseText">
               {{item.comments.storeResponse}}
              </p>
            </div>
            <div class="addResponse" v-else-if="item.comments.storeId===userStore.user.id">
                <button @click="storeResponse(index)">添加回复</button>
            </div>
          </div>
        </div>
      </li>
    </ul>
  </div>
  <template v-else>
    <el-empty description="无数据" />
  </template>

  <button ref="toResponse" style="display: none" type="button" class="btn btn-primary" data-bs-toggle="modal" data-bs-target="#response" data-bs-whatever="@mdo">打开</button>

</div>
</template>

<script setup>
import {ref, onMounted, watch} from 'vue'
import {useUserStore} from "@/stores/userStore";
import {getStoreCommentsAPI} from "@/apis/comments";
import {useRoute,useRouter} from "vue-router";
import {ElMessage} from "element-plus";
import {useCommentsStore} from "@/stores/commentsStore";
import {Constant} from "@/utils";
import {useWsStore} from "@/stores/wsStore";

const route=useRoute()
const router=useRouter()
const userStore=useUserStore();
const storeId=ref('')
const commentsListData=ref([])
const toResponse=ref()
const commentsStore=useCommentsStore()
const wsStore=useWsStore()

const pageData=ref({
  current:1
})

const searchData=ref({
  star:0,
  content:''
})

const getRemark=async ()=>{
  const res=await getStoreCommentsAPI({current:pageData.value.current,storeId:storeId.value,...searchData.value})
  if(res.data.code===1)
  {
    console.log(res.data)
    commentsListData.value=res.data.data
  }
  else
  {
    ElMessage.error(res.data.msg)
  }
}

const formatDate=(time)=>{
  const date=new Date(time)
  const year = date.getFullYear();
  let month = date.getMonth() + 1;
  let day = date.getDate();
  let hours = date.getHours();
  let minutes = date.getMinutes();
  let seconds = date.getSeconds();

  // 在月、日、小时、分钟、秒数小于10时，在前面补0
  month = month < 10 ? '0' + month : month;
  day = day < 10 ? '0' + day : day;
  hours = hours < 10 ? '0' + hours : hours;
  minutes = minutes < 10 ? '0' + minutes : minutes;
  seconds = seconds < 10 ? '0' + seconds : seconds;

  const formattedDate = `${year}-${month}-${day} ${hours}:${minutes}:${seconds}`;
  return formattedDate;
}

const orderInfo=(list)=>{
  if(!list) return ""
  console.log(list)
  return list.map(item=>{
    return ""+item.name+" "+item.flavors
  }).join(" | ")
}

const storeResponse=(index)=>{
  //处理回复
  let data=commentsListData.value[index]
  commentsStore.setResponse(data)

  toResponse.value.click()
}

watch(()=>wsStore.message,async ()=>{
  console.log(wsStore.message)
  // console.log(wsStore.message.value)

  if(wsStore.message?.messageType===Constant.Store_Remark_Success) {
    const message = wsStore.message.t
    //更新一下列表
    getRemark()

    ElMessage.success("发布成功")

    toResponse.value.click()
  }
  else if(wsStore.message?.messageType===Constant.Store_Remark_Fail)
  {
    ElMessage.error("发布失败")
    toResponse.value.click()
  }
},{
  deep:true
})

onMounted(()=>{
  wsStore.wsInit()
  storeId.value=route.params.id
  console.log(storeId.value)
  getRemark()
})
</script>

<style scoped>
ul
{
  padding-left:0;
}
.remark
{
  width: 100%;
  /*height: 300px;*/
  /*background: #01b6fd;*/
}
.remarkBigBox
{
  /*background: #f6f6f6;*/
}
.remarkItemBox ul li .remark
{
  box-sizing: border-box;
  background: white;
  /*padding:10px;*/
  margin-bottom: 30px;
  border: 1px dotted #7a8289;
  border-radius: 20px;
}
.remarkHeader
{
  display: flex;
  margin-left: 30px;
  /*background: #fed6e3;*/
  height: 100px;
  line-height: 100px;
  justify-content: space-between;

}
.remarkHeader img
{
  width: 70px;
  height: 70px;
  border-radius: 50%;
  margin-top: 15px;
}
.remarkHeader .remarkName
{
  margin-left: 30px;
  height: 100px;
  margin-top: 15px;
}
.remarkHeader .remarkName div
{
  height: 35px;
  line-height: 35px;
}
.remarkAvatar
{
  display: flex;
  margin-left: 30px;
}
.remarkHeader .remarkName div:nth-child(1)
{
  font-weight: bold;
  font-size: 18px;
}
.remarkHeader .remarkName div:nth-child(2)
{
  color: #7a8289;
  font-weight: bold;
}
.remarkHeader .remarkStars
{
  display: flex;
  margin-left: 30px;
  margin-right: 70px;
}
.remarkStars li
{
  margin-left: 5px;
  color:#01b6fd;
}
.remarkStarsText
{
  font-weight: bold;
  font-size: 17px;
}
.remarkText
{
  margin-left: 50px;
  font-size: 16px;
  margin-right: 50px;
  text-indent: 2em;
}
.remarkPic
{
  margin: 10px 50px;
  display: flex;
}
.remarkPic img
{
  width: 200px;
  height: 200px;
  margin-right: 10px;
}
.storeResponse
{
  box-sizing: border-box;
  background: #e3e3e3;
  margin-left: 50px;
  margin-right: 50px;
  border-radius: 10px;
  padding:10px;
  font-size: 15px;
  margin-bottom: 30px;
  text-indent: 2em;
}
.remarkSku
{
  display: flex;
  flex-wrap: wrap;
  margin: 0 50px;
  margin-bottom: 10px;
  font-size: 15px;
  color:#7a8289;
}
.remarkSku span
{
  margin-right: 10px;
}
.condition
{
  display: flex;
  height: 70px;
  margin-left: 20px;
  /*background-color: #02b7fb;*/
}
.condition input,
.condition select
{
  height: 40px;
  width: 200px;
  margin-right: 70px;
  /*line-height: 50px;*/
}
.condition label
{
  display: flex;
  line-height: 40px;
  width: 350px;
  /*background-color: #02b7fb;*/
  justify-content: space-between;

  /*font-weight: bold;*/
  /*margin-right: 40px;*/
}
.addResponse button
{
  margin-left: 50px;
  width: 120px;
  height: 50px;
  margin-bottom: 10px;
  border:1px solid #02b7fb;
  background-color: #4e9fe7;
  font-size: 16px;
  font-weight: bold;
  color: white;
  border-radius: 20px;

}
</style>